#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 50000
int fa[N],ran[N],n;
inline void init(){
	for(int i=1;i<=n;i++){
		fa[i]=i;
		ran[i]=0;
	}
}
int find(int x){
	if(x==fa[x])
		return x;
	int t=fa[x];
	fa[x]=find(fa[x]);
	ran[x]=(ran[x]+ran[t])%3;
	return fa[x];
}
inline void merge(int i,int j,int d){
	int x=find(i),y=find(j);
	if(x==y)	return;
	fa[y]=x;
	ran[y]=(3-ran[j]+d-1+ran[i])%3;
}
int main(){
	int m,a,b,d,i,ans=0;
	scanf("%d%d",&n,&m);
	init();
	while(m--){
		scanf("%d%d%d",&d,&a,&b);
		if(a>n||b>n||(d==2&&a==b))	ans++;
		else if(find(a)==find(b)){
			if(d==1&&ran[a]!=ran[b])	ans++;
			if(d==2&&(ran[a]+1)%3!=ran[b])	ans++;
		}
		else
			merge(a,b,d);
	}
	printf("%d\n",ans);
}

